#include "paintCursor.h"

PaintCursor::PaintCursor() {

   size = 20;
   number_points = 8;

   shader_cursor = new glslKernel();
   shader_cursor->vertex_source("shader_cursor.vert");
   shader_cursor->fragment_source("shader_cursor.frag");
   shader_cursor->install(true);
   shader_cursor->use(false);

   clicked = false;
   color = Color(1.0, 0.0, 0.0, 1.0);
}

PaintCursor::PaintCursor(GLint x, GLint y) {
   PaintCursor();
   sPosition = Point2i(x, y);
}

PaintCursor::~PaintCursor() {
}

void PaintCursor::updatePosition(GLint x, GLint y){
   // screen position
   sPosition = Point2i(x, y);
}

void PaintCursor::draw(GLuint prim){
   glDisable(GL_LIGHTING);
   glDisable(GL_DEPTH_TEST);
   glEnable(GL_TEXTURE_2D);

   shader_cursor->use(true);
   glBindTexture(GL_TEXTURE_2D, normalBuffer);
   glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

   shader_cursor->set_uniform("normalBuffer", 0);
   shader_cursor->set_uniform("primitive", (GLint) prim);
   shader_cursor->set_uniform("position", (GLfloat) sPosition[0], (GLfloat) sPosition[1]);
   shader_cursor->set_uniform("center", (GLfloat) sPosition[0]/winw, (GLfloat) sPosition[1]/winh);
   shader_cursor->set_uniform("nbSize", (GLfloat) winw, (GLfloat) winh);

   glColor3f(color[0], color[1], color[2]);
   glBegin(prim);
      for (int i = 0; i < number_points; i ++) {
         Point2i p((GLint) (sPosition[0] + size * cos(PI / 4 + 2 * i * PI / number_points)),
                   (GLint) (sPosition[1] + size * sin(PI / 4 + 2 * i * PI / number_points)));
         glTexCoord2d((GLfloat) p[0]/winw, (GLfloat) p[1]/winh);
         glVertex2i(p[0], p[1]);
      }
   glEnd();
   if (prim == GL_LINE_LOOP) {
      glBegin(GL_LINES);
         Point3 p((GLfloat) sPosition[0], (GLfloat) sPosition[1], (GLfloat) 2 * size);
         glTexCoord2d((GLfloat) p[0]/winw, (GLfloat) p[1]/winh);
         glVertex3f(p[0], p[1], 0.0);
         glTexCoord2d((GLfloat) p[0]/winw, (GLfloat) p[1]/winh);
         glVertex3f(p[0], p[1], p[2]);
      glEnd();
   }
   shader_cursor->use(false);

   glBindTexture(GL_TEXTURE_2D, 0);
   glDisable(GL_TEXTURE_2D);
   glEnable(GL_DEPTH_TEST);
   glEnable(GL_LIGHTING);
}

void PaintCursor::in(){
   if (size < 100) size = size + 1;
}

void PaintCursor::out(){
   if (size > 5) size = size - 1;
}


void PaintCursor::increase(){
   if (number_points < 32) number_points += 1;
}

void PaintCursor::decrease(){
   if (number_points > 3) number_points -= 1;
}
